﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using AMS.Configuration;

namespace AMS.UI.Controls
{
    /// <summary>
    /// 与字典绑定的下拉框包装器
    /// </summary>
    public class DictComboBoxDecorector
    {
        /// <summary>
        /// 与字典绑定的下拉框包装器. 使用key排序
        /// </summary>
        /// <param name="cmb"></param>
        /// <param name="dictKey"></param>
        public DictComboBoxDecorector(ComboBox cmb, int dictKey)
            : this(cmb, dictKey, DictItemSort.SortByKeyAscending)
        {
        }

        /// <summary>
        /// 与字典绑定的下拉框包装器，如果显示key则使用key排序，否则使用值排序
        /// </summary>
        /// <param name="cmb"></param>
        /// <param name="dictKey"></param>
        /// <param name="showSubKey"></param>
        public DictComboBoxDecorector(ComboBox cmb, int dictKey, bool showSubKey)
            : this(cmb, dictKey, showSubKey, showSubKey ? DictItemSort.SortByKeyAscending : DictItemSort.SortByNameAscending)
        {
        }

        /// <summary>
        /// 与字典绑定的下拉框包装器
        /// </summary>
        /// <param name="cmb"></param>
        /// <param name="dictKey"></param>
        /// <param name="sort"></param>
        public DictComboBoxDecorector(ComboBox cmb, int dictKey, DictItemSort sort)
            : this(cmb, dictKey, true, sort)
        {
        }

        /// <summary>
        /// 与字典绑定的下拉框包装器
        /// </summary>
        /// <param name="cmb"></param>
        /// <param name="dictKey"></param>
        /// <param name="showSubKey"></param>
        /// <param name="sort"></param>
        public DictComboBoxDecorector(ComboBox cmb, int dictKey, bool showSubKey, DictItemSort sort)
        {
            this.ComboBox = cmb;
            this.DictKey = dictKey;
            this.Sort = sort;
            this.ShowSubKey = showSubKey;
        }

        #region 属性
        /// <summary>
        /// 
        /// </summary>
        public ComboBox ComboBox { get; private set; }
        /// <summary>
        /// 字典号
        /// </summary>
        public int DictKey { get; private set; }
        /// <summary>
        /// 排序
        /// </summary>
        public DictItemSort Sort { get; private set; }
        /// <summary>
        /// 表明是否显示 key - 的形式
        /// </summary>
        public bool ShowSubKey { get; private set; }
        #endregion

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void DataBind()
        {
            ComboBox.Items.Clear();
            string[] items = null;
            if (ShowSubKey)
            {
                items = Dict.Instance.GetKeyValueStringList(DictKey, Sort);
            }
            else
            {
                items = Dict.Instance.GetKeyValueStringListWithoutSubKey(DictKey, Sort);
            }

            if (items == null)
            {
                throw new DictKeyNotFoundException(DictKey);
            }

            for (int i = 0; i < items.Length; i++)
            {
                ComboBox.Items.Add(items[i]);
            }
        }

        /// <summary>
        /// 在开始处插入
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void InsertAtFirst(string key, string value)
        {
            if (ShowSubKey)
            {
                InsertAt(0, string.IsNullOrEmpty(key) ? value : string.Format("{0} - {1}", key, value));
            }
            else
            {
                InsertAt(0, value);
            }
        }

        /// <summary>
        /// 在结束处插入
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void InsesrtAtLast(string key, string value)
        {
            if (ShowSubKey)
            {
                InsertAt(ComboBox.Items.Count, string.IsNullOrEmpty(key) ? value : string.Format("{0} - {1}", key, value));
            }
            else
            {
                InsertAt(ComboBox.Items.Count, value);
            }
        }

        private void InsertAt(int index, string text)
        {
            if (index == ComboBox.Items.Count)
            {
                ComboBox.Items.Add(text);
            }
            else
            {
                ComboBox.Items.Insert(index, text);
            }
        }

        /// <summary>
        /// 获取选中的项的key
        /// </summary>
        /// <returns></returns>
        public string GetSelectedDictKey()
        {
            if (ComboBox.Items.Count == 0 || ComboBox.SelectedIndex < 0)
            {
                return null;
            }

            string text = ComboBox.SelectedItem.ToString();
            if (string.IsNullOrEmpty(text))
            {
                return null;
            }

            if (ShowSubKey)
            {
                int index = text.IndexOf('-');
                if (index < 0)
                {
                    return null;
                }

                return text.Substring(0, index).Trim();
            }
            else
            {
                return Dict.Instance.GetDictKey(DictKey, text);
            }
        }

        /// <summary>
        /// 获取选中项的值
        /// </summary>
        /// <returns></returns>
        public string GetSelectedDictValue()
        {
            if (ComboBox.Items.Count == 0 || ComboBox.SelectedIndex < 0)
            {
                return null;
            }

            string text = ComboBox.SelectedItem.ToString();
            if (string.IsNullOrEmpty(text))
            {
                return null;
            }

            if (ShowSubKey)
            {
                int index = text.IndexOf('-');
                if (index < 0)
                {
                    return null;
                }

                return text.Remove(0, index).Trim();
            }
            else
            {
                return text;
            }
        }

        /// <summary>
        /// 选中下拉框中的一项，如果不存在相应的subKey则返回 -1
        /// </summary>
        /// <param name="subKey"></param>
        /// <returns></returns>
        public int SelectItem(string subKey)
        {
            if (ShowSubKey)
            {
                string searchKey = subKey + " -";
                for (int i = 0; i < ComboBox.Items.Count; i++)
                {
                    if (((string)ComboBox.Items[i]).StartsWith(searchKey))
                    {
                        ComboBox.SelectedIndex = i;
                        return i;
                    }
                }

                ComboBox.SelectedIndex = -1;
                return -1;
            }
            else
            {
                string value = Dict.Instance.GetDictValue(DictKey, subKey);
                ComboBox.SelectedItem = value;
                return ComboBox.SelectedIndex;
            }
        }
    }

}
